package edu.whu.puppygo.controller;

import com.fasterxml.jackson.annotation.JsonIgnore;
import edu.whu.puppygo.base.HttpResponse;
import edu.whu.puppygo.dao.PetDao;
import edu.whu.puppygo.dao.PetMatterDao;
import edu.whu.puppygo.dto.PetMatterDto;
import edu.whu.puppygo.entity.Pet;
import edu.whu.puppygo.entity.PetMatter;
import edu.whu.puppygo.entity.PetMatterClassificationEnum;
import edu.whu.puppygo.service.PetMatterService;
import edu.whu.puppygo.util.GlobalUser;
import edu.whu.puppygo.vo.PetMatterTodayVo;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author xinjiezhuang
 * @Date 2023-11-20
 */
@RestController
@Slf4j
@RequestMapping("/petmatters")
public class PetMatterController {
    private final PetDao petDao;

    public PetMatterController(PetDao petDao) {
        this.petDao = petDao;
    }

    @Resource
    private PetMatterService petMatterService;

    @Resource
    private PetMatterDao petMatterDao;

    // 获取当前用户所有的PetMatter
    @GetMapping("")
    public HttpResponse<List<PetMatter>> getAllPetMattersByUserId(int pageSize, int pageNum) {
        Long userId = GlobalUser.getUser().getId();
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        List<PetMatter> petMatters = petMatterService.getAllPetMattersByUserId(userId, pageRequest);
        return HttpResponse.success(petMatters);
    }

    // 获取今天应该执行的当前用户所有的PetMatter
    @GetMapping("/today")
    public HttpResponse<List<PetMatterTodayVo>> getTodayPetMattersByUserId(int pageSize, int pageNum) {
        Long userId = GlobalUser.getUser().getId();
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        List<PetMatter> petMatters = petMatterService.getTodayPetMattersByUserId(userId, pageRequest);
        List<PetMatterTodayVo> petMattersTodayVo = new ArrayList<>();
        for (PetMatter petMatter : petMatters) {
            PetMatterTodayVo petMatterTodayVo = new PetMatterTodayVo();
            petMatterTodayVo.setId(petMatter.getId());
            petMatterTodayVo.setUserId(petMatter.getUserId());
            petMatterTodayVo.setPetId(petMatter.getPetId());
            petMatterTodayVo.setTitle(petMatter.getTitle());
            petMatterTodayVo.setContent(petMatter.getContent());
            petMatterTodayVo.setClassification(petMatter.getClassification());
            petMatterTodayVo.setStartTime(petMatter.getStartTime());
            petMatterTodayVo.setFinishTime(petMatter.getFinishTime());

            Pet pet = petDao.findById(petMatter.getPetId()).get();
            petMatterTodayVo.setPetAvatar(pet.getPicture());
            petMatterTodayVo.setPetName(pet.getName());
            petMattersTodayVo.add(petMatterTodayVo);
        }

        return HttpResponse.success(petMattersTodayVo);
    }

    @GetMapping("/byPet")
    public HttpResponse<List<PetMatter>> getAllPetMattersByPetId(Long petId) {
        List<PetMatter> petMatters = petMatterDao.findByPetIdOrderByCreateTimeDesc(petId);
        return HttpResponse.success(petMatters);
    }

    @PostMapping("")
    public HttpResponse<PetMatter> addPetMatter(@RequestBody PetMatterDto petMatterDto) {
        log.info("petMatterDto: {}", petMatterDto);
        PetMatter addedPetMatter = petMatterService.addPetMatter(petMatterDto);
        return HttpResponse.success(addedPetMatter);
    }

    // 删除PetMatter
    @DeleteMapping("/{petMatterId}")
    public HttpResponse<String> deletePetMatter(@PathVariable Long petMatterId) {
        petMatterService.finishPetMatter(petMatterId);
        return HttpResponse.success("删除成功");
    }

    // 修改PetMatter，确保操作的是当前用户的数据
    @PutMapping("")
    public HttpResponse<PetMatter> updatePetMatter(@RequestBody PetMatter petMatter) {
        PetMatter updatedPetMatter = petMatterService.updatePetMatter(petMatter);
        return HttpResponse.success(updatedPetMatter);
    }

    @ApiOperation(value = "获取当天应该被提醒的事项，日期应为yyyy-MM-dd")
    @GetMapping("/findByDate")
    public HttpResponse<List<PetMatter>> findPetMattersByUserIdAndDate(@RequestParam Long userId, @RequestParam String date) {
        // 将前端传递的日期字符串转换为 LocalDate
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate;
        if (date == null) {
            localDate = LocalDate.now();
        } else {
            localDate = LocalDate.parse(date, formatter);
        }
        List<PetMatter> petMatters = petMatterService.findPetMattersByUserIdAndDate(userId, localDate);
        return HttpResponse.success(petMatters);
    }


}
